﻿using Microsoft.WindowsAPICodePack.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Interop;
using WpfModelEditor.Core.MVVM;

namespace WpfModelEditor.Core.Controls
{
    public class SelectPathTextBox : TextBox
    {
        public bool Multiselect
        {
            get { return (bool)GetValue(MultiselectProperty); }
            set { SetValue(MultiselectProperty, value); }
        }

        public static readonly DependencyProperty MultiselectProperty = DependencyProperty.Register("Multiselect", typeof(bool), typeof(SelectPathTextBox), new PropertyMetadata(false));


        public bool Open
        {
            get { return (bool)GetValue(OpenProperty); }
            set { SetValue(OpenProperty, value); }
        }

        public static readonly DependencyProperty OpenProperty = DependencyProperty.Register("Open", typeof(bool), typeof(SelectPathTextBox), new PropertyMetadata(true));


        public bool Folder
        {
            get { return (bool)GetValue(FolderProperty); }
            set { SetValue(FolderProperty, value); }
        }

        public static readonly DependencyProperty FolderProperty = DependencyProperty.Register("Folder", typeof(bool), typeof(SelectPathTextBox), new PropertyMetadata(false));


        public ICommand SelectPathCommand
        {
            get { return (ICommand)GetValue(SelectPathCommandProperty); }
            set { SetValue(SelectPathCommandProperty, value); }
        }

        public static readonly DependencyProperty SelectPathCommandProperty = DependencyProperty.Register("SelectPathCommand", typeof(ICommand), typeof(SelectPathTextBox));
        public SelectPathTextBox()
        {
            SelectPathCommand = new DelegateCommand(() =>
            {
                string fullFileName = this.Text;
                CommonFileDialog dialog;
                if (Open)
                {
                    dialog = new CommonOpenFileDialog();
                    ((CommonOpenFileDialog)dialog).IsFolderPicker = Folder;
                    if (!Folder)
                    {
                        ((CommonOpenFileDialog)dialog).Multiselect = Multiselect;
                    }
                }
                else
                {
                    dialog = new CommonSaveFileDialog();
                }

                dialog.EnsureFileExists = true;
                dialog.EnsurePathExists = true;
                dialog.InitialDirectory = fullFileName;

                var win = Window.GetWindow(this);
                if (dialog.ShowDialog(new WindowInteropHelper(win).Handle) != CommonFileDialogResult.Ok)
                    return;

                fullFileName = dialog.FileName;

                SetCurrentValue(TextProperty, fullFileName);
            });
        }
    }
}
